#!/bin/bash
# this is the boot-service init script for the NFS-Storage-hosts
#
# openQRM Enterprise developed by openQRM Enterprise GmbH.
#
# All source code and content (c) Copyright 2014, openQRM Enterprise GmbH unless specifically noted otherwise.
#
# This source code is released under the GNU General Public License version 2, unless otherwise agreed with openQRM Enterprise GmbH.
# The latest version of this license can be found here: src/doc/LICENSE.txt
#
# By using this software, you acknowledge having read this license and agree to be bound thereby.
#
#           http://openqrm-enterprise.com
#
# Copyright 2014, openQRM Enterprise GmbH <info@openqrm-enterprise.com>
#

OPENQRM_SERVER_BASE_DIR=$(pushd $(dirname $0)/../../../../.. > /dev/null; echo $PWD; popd > /dev/null)
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/nfs-storage/include/openqrm-plugin-nfs-storage-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/nfs-storage/etc/openqrm-plugin-nfs-storage.conf

# do not start on the intitrd
if [ -f /etc/initrd-devices.conf ]; then
	exit 0
fi

function nfs_storage_start() {
	echo "Starting the openQRM nfs-storage-plugin"
	if [ "$NFS_STORAGE_AUTO_START" == "yes" ]; then
		if [ -x "/etc/init.d/nfs-kernel-server" ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d nfs-kernel-server start
			else
				/etc/init.d/nfs-kernel-server start
			fi
		fi
		if [ -x "/etc/init.d/nfs" ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d nfs start
			else
				/etc/init.d/nfs start
			fi
		fi
		if [ -x "/etc/init.d/nfslock" ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d nfslock start
			else
				/etc/init.d/nfslock start
			fi
		fi
		# for Suse
		if [ -x /etc/init.d/nfsserver ]; then
			/etc/init.d/nfsserver start
		fi
	fi
}


function nfs_storage_stop() {
	echo "Stopping the openQRM nfs-storage-plugin"
	if [ "$NFS_STORAGE_AUTO_START" == "yes" ]; then
		if [ -x "/etc/init.d/nfs-kernel-server" ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d nfs-kernel-server stop
			else
				/etc/init.d/nfs-kernel-server stop
			fi
		fi
		if [ -x "/etc/init.d/nfs" ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d nfs stop
			else
				/etc/init.d/nfs stop
			fi
		fi
		if [ -x "/etc/init.d/nfslock" ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d nfslock stop
			else
				/etc/init.d/nfslock stop
			fi
		fi
		# for Suse
		if [ -x /etc/init.d/nfsserver ]; then
			/etc/init.d/nfsserver stop
		fi
	fi
}





case "$1" in
	start)
		nfs_storage_start
		;;
	stop)
		nfs_storage_stop
		;;
	restart)
		nfs_storage_stop
		sleep 1
		nfs_storage_start
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1

esac
exit $?

